<script type="text/javascript">
$(function(){
	$("input[name='ReadOldCitizenCardNo']").attr("hidden",true);
});

//检测卡
function checkCardsIsconsistent(newCitizenCardNo,oldCitizenCardNo){
	var checkResult=new Array();
	var option="";
	if(oldCitizenCardNo==null){
		option={newCitizenCardNo:newCitizenCardNo};
	}else{
		option={newCitizenCardNo:newCitizenCardNo,oldCitizenCardNo:oldCitizenCardNo};
	}
	$.ajax({
		url:"balanceTransfer/checkCardsIsconsistent",
		dataType:"json",
		async: false,
	    type: 'post',
	    data: option,
        success:function(json){
    		checkResult[0]=json.check_1
	  		if(json.check_2!="undefined"){
	  			checkResult[1]=json.check_2
	  		}
   		},
	})
	return checkResult;
}

//新卡
function readNewCardByTransfer(){
	var isRead=$("#isReadByTransfer").val();
	if(isRead==""){
		 alertMsg.warn("请先选择卡是否可读！")
		 return false;
	}
	var flag=qzsmkocx.activeCPU();
	if(flag){
		flag=qzsmkocx.GetSmkNo();
	}
	var isRead=$("#isReadByTransfer").val();
	var oldCitizenCardNo=$("input[name='oldCitizenCardNo']").val();
	var newCitizenCardNo="";
	if(flag){
		newCitizenCardNo=qzsmkocx.GetRetValue().split(",")[0];
		$("input[name='newCitizenCardNo']").val(newCitizenCardNo);
		$("input[name='citizenCardNo']").val(newCitizenCardNo);
	}else{
		checkError(getError());
		return false;
	}
	if(oldCitizenCardNo==""){//旧卡不可读
		var checkRes=checkCardsIsconsistent(newCitizenCardNo,null);
		if(checkRes[0]){
			if(isRead=="2"){
				$.post("balanceTransfer/getBalanceTransferInfo",{newCitizenCardNo:newCitizenCardNo,isRead:isRead},isNoRead,"json");
			}
			if(isRead=="1"){
				$.post("balanceTransfer/getBalanceTransferInfo",{newCitizenCardNo:newCitizenCardNo,isRead:isRead},isReadNewCard,"json");
			}
			
		}else{
			 alertMsg.warn("请读新卡,或此卡无需办理余额转移")
			 return false;
		}	
	}else if(oldCitizenCardNo!=""){
		var checkRes=checkCardsIsconsistent(newCitizenCardNo,oldCitizenCardNo);
		if(checkRes[0]&&checkRes[1]){
			if(isRead=="1"){
				$.post("balanceTransfer/getBalanceTransferInfo",{newCitizenCardNo:newCitizenCardNo,isRead:isRead},isReadNewCard,"json");
			}
			
		}else{
			 alertMsg.warn("请确认新卡与旧卡是否读反或者是否一致")
			 return false;
		}
	}
}

//旧卡
function readOldCardByTransfer(){
	var isRead=$("#isReadByTransfer").val();
	var flag=qzsmkocx.activeCPU();
	var newCitizenCardNo=$("input[name='newCitizenCardNo']").val();
	if(flag){
		flag=qzsmkocx.GetSmkNo();
	}
	var oldCitizenCardNo="";
	if(flag){
		oldCitizenCardNo=qzsmkocx.GetRetValue().split(",")[0];
		$("input[name='oldCitizenCardNo']").val(oldCitizenCardNo);
	}else{
		checkError(getError());
		return false;
	}
	if(newCitizenCardNo!=""&&isRead=="1"){
		var checkRes=checkCardsIsconsistent(newCitizenCardNo,oldCitizenCardNo);
		if(checkRes[0]&&checkRes[1]){
			$.post("balanceTransfer/getBalanceTransferInfo",{oldCitizenCardNo:oldCitizenCardNo,isRead:isRead},isReadOldCard,"json");
		}else{
			 alertMsg.warn("请确认新卡与旧卡是否读反或者是否一致")
			 return false;
		}
	}
	if(newCitizenCardNo==""&&isRead=="1"){
		var checkRes=checkCardsIsconsistent(oldCitizenCardNo,null);
		if(checkRes[0]){
			 alertMsg.warn("请读旧卡")
			 return false;
		}else{
			$.post("balanceTransfer/getBalanceTransferInfo",{oldCitizenCardNo:oldCitizenCardNo,isRead:isRead},isReadOldCard,"json");
		}
	}
	
}

//是否可读
 $("#isReadByTransfer").change(function(){
	var isRead=$("#isReadByTransfer").val();
	if(isRead=="2"){
		$("input[name='ReadOldCitizenCardNo']").attr("hidden",true);
		
	}
	if(isRead=="1"){
		$("input[name='ReadOldCitizenCardNo']").removeAttr("hidden");
	
	}
}); 

//不可读回调函数
function isNoRead(json){
	if(json.statusCode==DWZ.statusCode.error){
		navTabAjaxDone(json)
		return
	}
	$("input[name='oldCitizenCardNo']").val(json.bvo_no.oldCitizenCardNo);
	$("input[name='date']").val(getDate(json.bvo_no.date))
	var oldApps=json.bvo_no.map.oldCard;
	var newApps=json.bvo_no.map.newCard;
	var contenta="";
	var contentb="";
	for(var i=0;i<oldApps.length;i++){
		//alert(oldApps[i].totalBalance.toFixed(2))
		if(oldApps[i].totalBalance==0){
			contenta+="<tr  onmouseover='moveoverMouse(this)' onmouseout='moveoutMouse(this)' align='center'><td width='18%' >"+oldApps[i].appKindName+"</td><td width='17%'>"+oldApps[i].appName+"<input type='hidden' value="+oldApps[i].appType+"></td><td width='17%'>"+
			oldApps[i].walletTypeName+"</td><td width='16%'>"+oldApps[i].crdBalance.toFixed(2)+"</td><td width='16%' >"+oldApps[i].totalBalance.toFixed(2)+"</td><td width='16%'></td></tr>"
		}else{
			contenta+="<tr  onmouseover='moveoverMouse(this)' onmouseout='moveoutMouse(this)' align='center'><td width='18%'>"+oldApps[i].appKindName+"</td><td width='17%'>"+oldApps[i].appName+"<input type='hidden' value="+oldApps[i].appType+"></td><td width='17%'>"+
			oldApps[i].walletTypeName+"</td><td width='16%'>"+oldApps[i].crdBalance.toFixed(2)+"</td><td width='16%'>"+oldApps[i].totalBalance.toFixed(2)+"</td><td width='16%'><a id="+oldApps[i].appFlag+" href='#' onclick='return blanceTransferNoRead(this);'>转移</a></td></tr>"
		}	
	}
	for(var i=0;i<newApps.length;i++){
		var crdBalance,flag;
		flag=getBalance(newApps[i].appFlag, newApps[i].walletTypeName);
		if(flag){
			crdBalance=getValue().split(",")[0];
		}else{
			checkError(getError());
		}
		contentb+="<tr  onmouseover='moveoverMouse(this)' onmouseout='moveoutMouse(this)' align='center'><td width='20%' >"+newApps[i].appKindName+"</td><td width='20%'>"+newApps[i].appName+"<input type='hidden' value="+newApps[i].appFlag+"></td><td width='20%'>"+
		newApps[i].walletTypeName+"</td><td width='20%'>"+crdBalance+"</td><td width='20%'>"+newApps[i].currCount+"</td></tr>"		
	}
	$("#oldApp").html(contenta);
	$("#newApp").html(contentb);
}

//可读回调函数(新卡)
function isReadNewCard(json){
	if(json.statusCode==DWZ.statusCode.error){
		navTabAjaxDone(json)
	}else{
		$("input[name='date']").val(getDate(json.bvo_is_new.date))
		var newApps=json.bvo_is_new.map.newCard;
		var contentb="";
		for(var i=0;i<newApps.length;i++){
			var crdBalance,flag;
			flag=getBalance(newApps[i].appFlag, newApps[i].walletTypeName);
			if(flag){
				crdBalance=getValue().split(",")[0];
			}else{
				checkError(getError());
			}
			contentb+="<tr  onmouseover='moveoverMouse(this)' onmouseout='moveoutMouse(this)' align='center'><td width='20%' >"+newApps[i].appKindName+"</td><td width='20%'>"+newApps[i].appName+"<input type='hidden' value="+newApps[i].appFlag+"></td><td width='20%'>"+
			newApps[i].walletTypeName+"</td><td width='20%'>"+crdBalance+"</td><td width='20%'>"+newApps[i].currCount+"</td></tr>"		
		}
		$("#newApp").html(contentb);
	}	
}
//可读回调函数(旧卡)
function isReadOldCard(json){
	if(json.statusCode==DWZ.statusCode.error){
		navTabAjaxDone(json)
	}else{
		var oldApps=json.bvo_is_old.map.oldCard;
		var contenta="";
		for(var i=0;i<oldApps.length;i++){
			var crdBalance,flag;
			flag=getBalance(oldApps[i].appFlag, oldApps[i].walletTypeName);
			if(flag){
				crdBalance=getValue().split(",")[0];
			}else{
				checkError(getError());
			}
			if(crdBalance=="0.00"){
				contenta+="<tr  onmouseover='moveoverMouse(this)' onmouseout='moveoutMouse(this)' align='center'><td width='18%' >"+oldApps[i].appKindName+"</td><td width='17%'>"+oldApps[i].appName+"</td><td width='17%'>"+
				oldApps[i].walletTypeName+"</td><td width='16%'>"+crdBalance+"</td><td width='16%' >"+oldApps[i].totalBalance+"</td><td width='16%'><input name='refreshA' type='hidden' value=''></td></tr>"
			}else{
				contenta+="<tr  onmouseover='moveoverMouse(this)' onmouseout='moveoutMouse(this)' align='center'><td width='18%'>"+oldApps[i].appKindName+"</td><td width='17%'>"+oldApps[i].appName+"</td><td width='17%'>"+
				oldApps[i].walletTypeName+"</td><td width='16%'>"+crdBalance+"</td><td width='16%'>"+oldApps[i].totalBalance+"</td><td width='16%'><a id="+oldApps[i].appFlag+" href='#' onclick='return blanceTransferFromOld(this);'>消费</a>|<a id="+oldApps[i].appFlag+" href='#' onclick='return blanceTransferToNew(this);'>充值</a></td></tr>"
			}	
		}
		$("#oldApp").html(contenta);
	}
	
}

function blanceTransferNoRead(a){
	var newCitizenCardNo=$("input[name='newCitizenCardNo']").val();
	var oldCitizenCardNo=$("input[name='oldCitizenCardNo']").val();
	var walletType=$(a).parent().parent().find("td:eq(2)").text();
	var money=$(a).parent().parent().find("td:eq(4)").text();
	var appType=$(a).parent().parent().find("td:eq(1) input").val();
	var adf=$(a).attr("id");
	var f=false;
	var newAppRows=$("#newApp").find("tr input");
	$(newAppRows).each(function(n,item){
		if($(this).val()==adf){
			f=true;
			return;
		}
	});
	if(f){
		var flag=qzsmkocx.activeCPU();
		if(flag){
			flag=qzsmkocx.GetSmkNo();
		}
		var citizenCardNo="";
		if(flag){
			citizenCardNo=qzsmkocx.GetRetValue().split(",")[0];
			if(citizenCardNo==newCitizenCardNo){
				//alert(adf+","+walletType+","+money)
				pay(adf,walletType,money)
				if(ff){
					//发送请求进行卡消费
					var option ={walletType:walletType,appFlag:adf,appType:appType,citizenCardNo:oldCitizenCardNo,txnAmt:money}
					$.post("balanceTransfer/updateblanceForOldCard",option,function(json){
							if(json.flag){
								$("input[name='oldCitizenCardNo']").val("")
								readNewCardByTransfer();
							}	
					},"json");
				}
			}				
			else{
				alertMsg.warn("请换新卡进行余额转移")
			}
		}
	}
	else{
		alertMsg.warn("无对应的应用可以转移")
	}
	return false;
}

function blanceTransferFromOld(a){
	var refreshA=$("input[name='refreshA']").val();
	if(refreshA!="1"){
		alertMsg.warn("请先对新卡充值")
		return false;
	}
	var oldCitizenCardNo=$("input[name='oldCitizenCardNo']").val();
	var walletType=$(a).parent().parent().find("td:eq(2)").text();
	var money=$(a).parent().parent().find("td:eq(3)").text();
	var adf=$(a).attr("id");
	var f=false;
	var newAppRows=$("#newApp").find("tr input");
	$(newAppRows).each(function(n,item){
		if($(this).val()==adf){
			f=true;
			return;
		}
	});
	if(f){
		var flag=qzsmkocx.activeCPU();
		if(flag){
			flag=qzsmkocx.GetSmkNo();
		}
		var citizenCardNo="";
		if(flag){
			citizenCardNo=qzsmkocx.GetRetValue().split(",")[0];
			if(citizenCardNo==oldCitizenCardNo){
				//alert("消费咔咔")
				//alert(adf+","+walletType+","+money)
				rechargeCancel(adf,walletType,money)
				readOldCardByTransfer();
			}else{
				alertMsg.warn("请换旧卡消费")
				return false;
			}
		}
		
	}else{
		alertMsg.warn("无对应的应用可以转移")
		return false;
	}
	return false;
}

function blanceTransferToNew(a){
	ff=false;
	if(again){
		alertMsg.warn("新卡已充值")
		return false;
	}
	var newCitizenCardNo=$("input[name='newCitizenCardNo']").val();
	var oldCitizenCardNo=$("input[name='oldCitizenCardNo']").val();
	var isRead=$("#isReadByTransfer").val();
	var walletType=$(a).parent().parent().find("td:eq(2)").text();
	var money=$(a).parent().parent().find("td:eq(3)").text();
	var adf=$(a).attr("id");
	var f=false;
	var newAppRows=$("#newApp").find("tr input");
	$(newAppRows).each(function(n,item){
		if($(this).val()==adf){
			f=true;
			return;
		}
	});
	if(f){
		var flag=qzsmkocx.activeCPU();
		if(flag){
			flag=qzsmkocx.GetSmkNo();
		}
		var citizenCardNo="";
		if(flag){
			citizenCardNo=qzsmkocx.GetRetValue().split(",")[0];
			if(citizenCardNo==newCitizenCardNo){
				//alert("充值比哟")
				//alert(adf+","+walletType+","+money)
				pay(adf,walletType,money)
				if(ff){//充值成功
					$("input[name='refreshA']").val("1")
					readNewCardByTransfer();
					again=true;
				}
			}else{
				alertMsg.warn("请换新卡充值")
				return false;
			}
		}
		
	}else{
		alertMsg.warn("无对应的应用可以转移")
		return false;
	}
	
	return false;
}

function cleanInput(){
	$("input[name='citizenCardNo']").val("")
	$("input[name='newCitizenCardNo']").val("")
	$("input[name='oldCitizenCardNo']").val("")
	$("input[name='date']").val("")
	$("#isReadByTransfer").val("")
	
}
</script>

<div class="pageContent">
	<form id="commitExchangeInfo" method="post" action="exchangeCard/commitExchangeInfo" class="required-validate" onsubmit="return validateCallback(this,navTabAjaxDone);">
		<div class="pageFormContent" layoutH="76"> 
		
			<fieldset>
			<legend>读卡区</legend>
			<p>
				<label>新市民卡号：</label>
				<input  type="text" class="" size="30"name="newCitizenCardNo" readonly="readonly" />
				&nbsp;&nbsp;<input type="button" class="newButton" value="读卡" onclick="readNewCardByTransfer()"/>
			</p>
			<input  type="hidden" name="citizenCardNo" readonly="readonly" />
			<p>
				<label>原卡是否可读：</label>
				<select id="isReadByTransfer" name="isRead">
					<option value="">请选择</option>
					<option value="2">不 可读</option>
					<option value="1">可读</option>
				</select>
			</p>
			<p>
				<label>旧市民卡号：</label>
				<input type="text"   name="oldCitizenCardNo" size="30" readonly="readonly"/>
				&nbsp;&nbsp;<input name="ReadOldCitizenCardNo" type="button" class="newButton"value="读卡" onclick="readOldCardByTransfer()"/>
			</p>
			 <p>
		        <label>补换卡日期：</label>
		        <input class="date" type="text" name="date" />
		        &nbsp;&nbsp;<input type="button" value="重置" class="newButton" onclick="cleanInput()"/>
            </P>
            </fieldset>
            
            <fieldset>
			<legend>新卡信息</legend>
            <table class="table" width="100%" layoutH="400">
			<thead>
				<tr align="center">
			<!-- 		<th width="2%"><input type="checkbox"  name="branchManage" group="branchManage" class="checkboxCtrl"></th> -->
					<th width="20%" >应用大类</th>
					<th width="20%" >应用子类</th>
					<th width="20%" >钱包类型</th>
					<th width="20%" >卡面余额</th>
					<th width="20%" >计数器</th>
				</tr>
			</thead>
			<tbody id="newApp">
						
			</tbody>
		</table>
			</fieldset>
			
			<fieldset>
			<legend>可转移账户</legend>
				<table class="table" width="100%" layoutH="400">
			<thead>
				<tr align="center">
			<!-- 		<th width="2%"><input type="checkbox"  name="branchManage" group="branchManage" class="checkboxCtrl"></th> -->
					<th width="18%" >应用大类</th>
					<th width="17%" >应用子类</th>
					<th width="17%" >钱包类型</th>
					<th width="16%" >卡面余额</th>
					<th width="16%" >账面余额</th>
					<th width="16%" >操作</th>
				</tr>
			</thead>
			<tbody id="oldApp">
						
			</tbody>
		</table>
			</fieldset>
			
			
		</div>
	</form>
</div>
